package com.gongpingjialoan.adapter.car;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.ViewPagerActivity;
import com.gongpingjialoan.manage.PointRecordManage;

/**
 * Created by Administrator on 2015/9/22.
 */
public class DisountCarDetailAdapter extends PagerAdapter {

    private String[] urls;

    private Fragment fragment;

    private Context mContext;

    private boolean isCanClick;


    public DisountCarDetailAdapter(String[] urls, Fragment fragment, boolean isClick) {
        this.urls = urls;
        this.fragment = fragment;
        this.mContext = fragment.getActivity();
        this.isCanClick = isClick;
    }


    public DisountCarDetailAdapter(String[] urls, Context context, boolean isClick) {
        this.urls = urls;
        this.mContext = context;
        this.isCanClick = isClick;
    }

    @Override
    public int getCount() {
        if (urls != null)
            return urls.length;
        else return 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        final ImageView iv = new ImageView(mContext);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        iv.setLayoutParams(lp);
        iv.setImageResource(R.drawable.car_loading);
        String url = urls[position] + "?imageMogr2/thumbnail/!640x360r";
        if (fragment != null) {
            Glide.with(fragment).load(url).centerCrop().placeholder(R.drawable.car_loading).error(R.drawable.car_no).into(iv);
        } else {
            Glide.with(mContext).load(url).centerCrop().placeholder(R.drawable.car_loading).error(R.drawable.car_no).into(iv);
        }


        if (isCanClick) {
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (null != urls && urls.length > 0) {
                        Intent intent = new Intent();
                        intent.putExtra("imgurls", urls);
                        intent.setClass(mContext, ViewPagerActivity.class);
                        intent.putExtra("currentItem", position);
                        mContext.startActivity(intent);
                        PointRecordManage.getInstance().addPoint(mContext, "CarDetailViewPhoto");

                    }
                }
            });
        }
        container.addView(iv, 0);
        return iv;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
